package com.nanachi.feign.core.config;

import com.nanachi.feign.core.LoadBalanceInterceptor;
import com.nanachi.feign.core.context.SimpleFeignContext;
import com.nanachi.feign.core.context.SimpleFeignSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author yuhongtai
 * 目的：将配置类放入SimpleFeignContext中
 */
@Configuration
public class AutoConfig {


    // 获取每个feign的配置
    @Autowired
    List<SimpleFeignSpecification> simpleFeignSpecifications;

    @Bean
    public SimpleFeignContext simpleFeignContext(){
        SimpleFeignContext context = new SimpleFeignContext();
        context.setConfigurations(simpleFeignSpecifications);
        return context;
    }

    /**
     * 配置拦截器
     * @return
     */
    @Bean
    public LoadBalanceInterceptor loadBalanceInterceptor(){
        return new LoadBalanceInterceptor();
    }

    /*@Bean
    public LoadBalancerInterceptor loadBalancerInterceptor(){
        return new LoadBalancerInterceptor();
    }*/

    /**
     * 配置restTemplate
     * @param
     * @return
     */
    // @CustomLoadBalance
    @LoadBalanced
    @Bean(name = "simpleFeignTemplate")
    public RestTemplate simpleFeignTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        //restTemplate.setInterceptors(Collections.singletonList(interceptor));
        return restTemplate;
    }




}
